package cn.itcast.service.impl;

import cn.itcast.dao.MemberDao;


import cn.itcast.entity.PageResult;
import cn.itcast.pojo.Member;
import cn.itcast.service.MemberService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;

import cn.itcast.pojo.Member;
import cn.itcast.service.MemberService;
import cn.itcast.utils.MD5Utils;
import com.alibaba.dubbo.config.annotation.Service;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Slf4j

@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired

    private MemberDao memberDao;

    @Override
    public void add(Member member) {
        memberDao.add(member);
    }

    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        Page<Member> page = memberDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public Member findById(Integer id) {
        return memberDao.findById(id);
    }

    @Override
    public void update(Member member, Integer[] memberIds) {
        memberDao.update(member);
    }

    @Override
    public void deleteById(Integer id) {
        memberDao.deleteById(id);
    }

    @Override
    public List<Member> findAll() {
        return memberDao.findAll();
    }

    //根据月份查询会员数量
    @Override
    public List<Integer> findMemberCountByMonths(List<String> months) {
        List<Integer> memberCount = new ArrayList<>();
        for (String month : months) {
            String date = month + ".31";
            Integer count = memberDao.findMemberCountBeforeDate(date);
            memberCount.add(count);
        }
        return memberCount;
    }

    @Override
    public Member login(String phone, String validateCode) {
        
        String rediskey="REDIS_KEY_"+phone;
        //从redis中取出验证码
        String redisCode = redisTemplate.opsForValue().get(rediskey);
        //校验验证码
        if(!StringUtils.equals(redisCode,validateCode)){
            //不一致,则返回空
            return null;
        }
        //如果一致要把redis中的验证码给删除掉
        redisTemplate.delete(rediskey);

        Member member = this.findByphoneNumber(phone);
        //如果为空自动注册
        if(null==member){
            member=new Member();
            member.setRegTime(new Date());
            member.setPhoneNumber(phone);
            this.saveMember(member);
        }
        return member;
    }

    @Override
    public Member findByphoneNumber(String phone) {
        
        return memberDao.findByphoneNumber(phone);
    }

    @Override
    public void saveMember(Member member) {
        
        String password = member.getPassword();
        if(password!=null){
            password=MD5Utils.md5(password);
            member.setPassword(password);
        }
        memberDao.saveMember(member);
    }
}

